﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Windows.Forms;
using ResxCrunch.Utils;
using System.IO;

namespace ResxCrunch
{
    /// <summary>
    /// Select files form class
    /// </summary>
    public partial class FrmSelectFiles : Form
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="FrmSelectKeys"/> class.
        /// </summary>
        public FrmSelectFiles()
        {
            InitializeComponent();
        }

        #region Properties
        /// <summary>
        /// Gets or sets the file table.
        /// </summary>
        /// <value>The file table.</value>
        public DataTable FileTable
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets the selected files
        /// </summary>
        /// <value>List containing the selected files.</value>
        public DataTable SelectedFileTable
        {
            get;
            set;
        }

        /// <summary>
        /// Sets a value indicating whether this <see cref="FrmMain"/> is busy.
        /// </summary>
        /// <value><c>true</c> if busy; otherwise, <c>false</c>.</value>
        public bool Busy
        {
            set
            {
                if (value)
                {
                    lblBusy.Visible = true;
                    this.Refresh();
                }
                else
                {
                    lblBusy.Visible = false;
                }
            }
        }
        #endregion Properties

        /// <summary>
        /// Handles the Load event of the FrmSelectFiles control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void FrmSelectFiles_Load(object sender, EventArgs e)
        {

        }

        /// <summary>
        /// Updates the values.
        /// </summary>
        private void UpdateValues()
        {
            lblNrSelected.Text = string.Concat(dgFiles.SelectedRows.Count, "/", dgFiles.RowCount, " selected");
        }

        /// <summary>
        /// Loads the files from the datatable
        /// </summary>
        private void LoadFiles()
        {
            this.Busy = true;

            string lastRowDir = string.Empty;
            string lastRowFile = string.Empty;

            if (this.FileTable.Rows.Count > 0 && 
                this.FileTable.Columns.Contains(Enums.DataColumn.Directory) && 
                this.FileTable.Columns.Contains(Enums.DataColumn.File))
            {
                for (int i = this.FileTable.Rows.Count - 1; i >= 0; i--)
                {
                    if (this.FileTable.Rows[i][Enums.DataColumn.Directory].ToString().Equals(lastRowDir) &&
                        this.FileTable.Rows[i][Enums.DataColumn.File].ToString().Equals(lastRowFile))
                    {
                        this.FileTable.Rows[i].Delete();
                    }
                    else
                    {
                        lastRowDir = this.FileTable.Rows[i][Enums.DataColumn.Directory].ToString();
                        lastRowFile = this.FileTable.Rows[i][Enums.DataColumn.File].ToString();
                    }
                }

                FileTable.AcceptChanges();
            }

            dgFiles.DataSource = this.FileTable;

            foreach (DataGridViewColumn dc in dgFiles.Columns)
            {
                if (!dc.Name.Equals(Enums.DataColumn.Directory) && 
                    !dc.Name.Equals(Enums.DataColumn.File))
                {
                    dc.Visible = false;
                }
            }

            if (dgFiles.Columns.Contains(Enums.DataColumn.Directory))
            {
                dgFiles.Sort(dgFiles.Columns[Enums.DataColumn.Directory], System.ComponentModel.ListSortDirection.Ascending);
            }

            this.Busy = false;
        }

        /// <summary>
        /// Handles the Click event of the btnOk control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void btnOk_Click(object sender, EventArgs e)
        {
            DataTable resultTable = this.FileTable.Clone();

            foreach (DataGridViewRow dr in dgFiles.SelectedRows)
            {
                DataRow newRow = resultTable.NewRow();
                newRow[Enums.DataColumn.Directory] = dr.Cells[Enums.DataColumn.Directory].Value.ToString();
                newRow[Enums.DataColumn.File] = dr.Cells[Enums.DataColumn.File].Value.ToString();

                resultTable.Rows.Add(newRow);
            }

            this.SelectedFileTable = resultTable;
            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        /// <summary>
        /// Handles the SelectedValueChanged event of the lstFiles control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void lstFiles_SelectedValueChanged(object sender, EventArgs e)
        {
            this.UpdateValues();
        }

        /// <summary>
        /// Handles the Shown event of the FrmSelectFiles control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void FrmSelectFiles_Shown(object sender, EventArgs e)
        {
            this.LoadFiles();
            this.UpdateValues();
        }

        private void dgFiles_SelectionChanged(object sender, EventArgs e)
        {
            this.UpdateValues();
        }
    }
}
